/*
 * Copyright 2008 Pretty-Tools.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Depends on: Configuration.js

var Components = {
    interfaces: {
        nsIPropertyElement: {
            toString: function() {
                return "{B4089FA6-4FA3-4141-BAFF-615FB472A7E2}";
            }
        },
        nsIFile: {
            NORMAL_FILE_TYPE: 0,
            DIRECTORY_TYPE: 1,
            toString: function() {
                return "{F75B5C32-B42D-416D-93AC-8798869674CE}";
            }
        },
        nsIFilePicker: {
            modeOpen      : 0,
            modeSave      : 1,
            modeGetFolder : 2,
            modeOpenMultiple : 3,
            returnOK      : 0,
            returnCancel  : 1,
            returnReplace : 2,
            filterAll     : 1,
            filterHTML    : 2,
            filterText    : 4,
            filterImages  : 8,
            filterXML     : 16,
            filterXUL     : 32,
            filterApps    : 64
        },
        nsIFileInputStream : {
            toString: function() {
                return "{CC3FD06A-0121-4041-A088-595BE5E4976B}";
            }
        },
        nsIInputStream : {
            toString: function() {
                return "{D08EE107-5DC5-4637-A81F-DDF47112D1F6}";
            }
        },
        nsISeekableStream: {
            NS_SEEK_SET: 0,
            NS_SEEK_CUR: 1,
            NS_SEEK_END: 2,
            toString: function() {
                return "{DDD3BB89-9A10-4e34-9A9E-D4E3D0EF7CE2}";
            }
        },
        nsIOutputStream: {
            toString: function() {
                return "{D974C538-C226-4f75-A032-F04BD86D8EA6}";
            }
        },
        nsISafeOutputStream: {
            toString: function() {
                return "{F4DD0575-726A-457f-9F8C-AA442674A2AB}";
            }
        },
        nsIFileOutputStream: {
            toString: function() {
                return "{4FD1401A-255E-4B95-9665-38D16B2E52CE}";
            }
        },
        nsIDOMXMLDocument: {
            toString: function() {
                return "{2933BF81-7B36-11d2-B20E-00C04F983E60}";
            }
        },
        nsIDOMNode: {
            ELEMENT_NODE:           1,
            ATTRIBUTE_NODE:         2,
            TEXT_NODE:              3,
            CDATA_SECTION_NODE:     4,
            ENTITY_REFERENCE_NODE:  5,
            ENTITY_NODE:            6,
            PROCESSING_INSTRUCTION_NODE: 7,
            COMMENT_NODE:           8,
            DOCUMENT_NODE:          9,
            DOCUMENT_TYPE_NODE:     10,
            DOCUMENT_FRAGMENT_NODE: 11,
            NOTATION_NODE:          12
        }
    }
};

function dump(value) {
    // nothing here
}

var yrprxComponentsFactory = {};

yrprxComponentsFactory.componentExists = function(CID)
{
    try
    {
        new ActiveXObject(CID);
        return true;
    }
    catch(ex)
    {
        return false;
    }
}

yrprxComponentsFactory.implementsInterface = function(obj, iid)
{
    try
    {
        if (typeof obj.Implements != 'undefined')
            return obj.Implements(iid);

        if (!this.finder)
            this.finder = yrprxComponentsFactory.createInstance("XPCOMAdapter.InterfaceFinder.1");

        return this.finder.Implements(obj, iid);
    }
    catch(ex)
    {
        return false;
    }
}

yrprxComponentsFactory.createInstance = function(CID)
{
    return new ActiveXObject(CID);
}

yrprxComponentsFactory.createWindowManager = function()
{
    return this.createInstance("XPCOMAdapter.WindowManager.1");
}

yrprxComponentsFactory.getPreferencesService = function()
{
    return this.createInstance("XPCOMAdapter.PrefService.1");
}

yrprxComponentsFactory.getPreferencesServiceAsBranch = function()
{
    return this.createInstance("XPCOMAdapter.PrefBranch.1");
}

yrprxComponentsFactory.getStringBundleService = function()
{
    return this.createInstance("XPCOMAdapter.StringBundleService.1");
}

yrprxComponentsFactory.createXMLDocument = function(async)
{
    var xmlDocument = this.createInstance("Microsoft.XMLDOM");

    xmlDocument.async = !!async;

    return xmlDocument;
}

yrprxComponentsFactory.implementsIDOMXMLDocument = function(obj)
{
    return this.implementsInterface(obj, Components.interfaces.nsIDOMXMLDocument);
}

yrprxComponentsFactory.createDOMParser = function()
{
    return this.createInstance("XPCOMAdapter.DOMParser.1");
}

yrprxComponentsFactory.createXMLSerializer = function()
{
    return this.createInstance("XPCOMAdapter.DOMSerializer.1");
}

yrprxComponentsFactory.createLocalFile = function(path)
{
    var file = this.createInstance("XPCOMAdapter.LocalFile.1");

    if (typeof path != "undefined") file.initWithPath(path);

    return file;
}

yrprxComponentsFactory.implementsIFile = function(obj)
{
    return this.implementsInterface(obj, Components.interfaces.nsIFile);
}

yrprxComponentsFactory.getDirectoryServiceAsProperties = function()
{
    return this.createInstance("XPCOMAdapter.DirectoryService.1");
}

yrprxComponentsFactory.createFileInputStream = function(file, ioFlags, perm, behaviorFlags)
{
    var inputStream = this.createInstance("XPCOMAdapter.FileInputStream.1");

    if (typeof ioFlags == "undefined") ioFlags = 0x01;
    if (typeof perm    == "undefined") perm    = 0x00;
    if (typeof behaviorFlags == "undefined") behaviorFlags = 0x00;

    inputStream.init(file, ioFlags, perm, behaviorFlags);

    return inputStream.QueryInterface(Components.interfaces.nsIInputStream);
}

yrprxComponentsFactory.implementsIInputStream = function(obj)
{
    return this.implementsInterface(obj, Components.interfaces.nsIInputStream);
}

yrprxComponentsFactory.createSafeFileOutputStream = function(file, ioFlags, perm, behaviorFlags)
{
    var outputStream = this.createInstance("XPCOMAdapter.FileOutputStream.1");

    if (typeof ioFlags == "undefined") ioFlags = 0x02 | 0x08 | 0x20;
    if (typeof perm    == "undefined") perm    = 0/*0666*/;
    if (typeof behaviorFlags == "undefined") behaviorFlags = 0x00;

    outputStream.init(file, ioFlags, perm, behaviorFlags);

    return outputStream;
}

yrprxComponentsFactory.implementsIInputStream = function(obj)
{
    return this.implementsInterface(obj, Components.interfaces.nsISafeOutputStream);
}

yrprxComponentsFactory.createFilePicker = function()
{
    return this.createInstance("XPCOMAdapter.FilePicker.1");
}

yrprxComponentsFactory.createWebBrowserPersist = function()
{
    return this.createInstance("XPCOMAdapter.WebBrowserPersist.1");
}

yrprxComponentsFactory.getObserverService = function()
{
    return null;//TODO: implement me - nsIObserverService
}
